package com.service.tools.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;

/**
 * @Author tangzuqiang
 * @Email 18982975988@189.cn
 * @Date 2021/01/14 下午 5:50
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    @JSONField(name = "error_code")
    private int code = 0;
    @JSONField(name="error_message")
    private String message = "服务器繁忙,请稍后再试!";
    private Object data = new ArrayList<>();
    private Integer timestamp;

    private Integer status;

    public Result(Object data, String message) {
        this.data = data;
        this.message = message;
        this.code = 200;
        this.status = this.getCode();
//        if(HttpUtils.requestType().equals("GET")){
//            this.timestamp = Integer.parseInt(Long.toString(
//                    new Date().getTime() / 1000
//            ));
//        }
    }

    public Result(int code, String message) {
        this.code = code;
        this.status = code;
        this.message = message;
        this.data = new ArrayList<>();
    }

    /* *
     * @Description  返回正确数据
     * @Author       tangzuqiang
     * @Email        18982975988@189.cn
     * @CreateTime   2021/01/14 下午 5:56
     * @Param data:  [message] [data,message]
     * @Param message:
     * @return     com.xhd.xhd.utils.Result
     */
    public static Result success(Object... data) {
        String message = "";
        Object resultData = new ArrayList<>();
        switch (data.length) {
            case 0:
                message = "操作成功";
                break;
            case 1:
                message = data[0].toString();
                break;
            case 2:
                message = data[0].toString();
                resultData = data[1];
        }
        return new Result(resultData, message);
    }



    /* *
     * @Description  返回错误数据
     * @Author       tangzuqiang
     * @Email        18982975988@189.cn
     * @CreateTime   2021/01/14 下午 5:57
     * @Param      参数1个:message 参数2个 code,message
     * @return     com.xhd.xhd.utils.Result
     */
    public static Result error(Object... args) {
//        String m = String.format("api[%s]错误返回信息",HttpUtils.getApiurl());
        if (args.length == 1) {
//            MongodbUtils.save(m, "error", args[0].toString());
            return new Result(0, args[0].toString());
        } else {
//            MongodbUtils.save(m, "error", String.format("code:%s;message:%s",
//                    args[0].toString(), args[1].toString()));
            return new Result(Integer.parseInt(args[0].toString()), args[1].toString());
        }
    }

    /* *
     * @Description  返回json格式字符串
     * @Author       tangzuqiang
     * @Email        18982975988@189.cn
     * @CreateTime   2021/01/15 下午 2:40
     * @Param result:
     * @return     java.lang.String
     */
    public static String toJson(Result result) {
        return JSON.toJSONString(result);
    }

}
